package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.CaiDanDao;
import net.mysoft.budgetsoft.system.domain.TCaiDan;
import net.mysoft.budgetsoft.system.domain.comparator.CaiDanComparator;
import net.mysoft.budgetsoft.system.pagemodel.CaiDan;
import net.mysoft.budgetsoft.system.pagemodel.DictionaryDetail;
import net.mysoft.budgetsoft.system.service.CaiDanService;
import net.mysoft.framework.cache.Cache;
import net.mysoft.framework.cache.CacheManager;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-12-27 08:45:08 
 */
@Service("caiDanService")
public class CaiDanServiceImpl implements CaiDanService{
	@Resource
	private CaiDanDao caiDanDao;
	
	public CaiDanDao getCaiDanDao(){
		return this.caiDanDao;
	}
	
	public void setCaiDanDao(CaiDanDao caiDanDao){
		this.caiDanDao = caiDanDao;
	}
	
	@Override
	public void add(CaiDan caiDan){
		TCaiDan t = new TCaiDan();
		BeanUtils.copyProperties(caiDan, t);
		t.setBianHao(UUID.randomUUID().toString());
		TCaiDan pt = new TCaiDan();
		pt.setBianHao(caiDan.getShangJiCDBH());
		t.setpCaiDan(pt);
		caiDanDao.save(t);
	}
	
	@Override
	public void edit(CaiDan caiDan){
		TCaiDan t = new TCaiDan();
		BeanUtils.copyProperties(caiDan, t);
		TCaiDan pt = new TCaiDan();
		pt.setBianHao(caiDan.getShangJiCDBH());
		t.setpCaiDan(pt);
		caiDanDao.update(t);
	}
	
	@Override
	public void delete(CaiDan caiDan) {
		for (String id : caiDan.getIds().split(",")) {
			Object t =  caiDanDao.get(TCaiDan.class, id);
			if (t != null) {
				 caiDanDao.delete(t);
			}
		}
	}
	
	@Override
	public DataGrid datagrid(CaiDan caiDan){
		DataGrid dg = new DataGrid();
		String hql = "from TCaiDan t where 1=1 and t.bianHao <> '0' ";
		List params = new ArrayList();
		hql = addWhere(caiDan, hql, params);
		hql = addOrder(caiDan, hql);
		String totalHql = "select count(*) " + hql;
		List<TCaiDan> toList = caiDanDao.find(hql, params, caiDan.getPage(), caiDan.getRows());
		List<CaiDan> oList = new ArrayList<CaiDan>();
		oList = changeModel(toList);
		dg.setTotal(caiDanDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}
	
	@Override
	public List<CaiDan> find(CaiDan caiDan) {
		String hql = "from TCaiDan t where 1=1";
		List paramList = new ArrayList();
		hql = addWhere(caiDan, hql, paramList);
		List<TCaiDan> toList = caiDanDao.find(hql, paramList);
		Collections.sort(toList, new CaiDanComparator());
		List<CaiDan> oList = new ArrayList<CaiDan>();
		oList = changeModel(toList);
		return oList;
	}

	@Override
	public List<CaiDan> treegrid(CaiDan caiDan) {
		String hql = "from TCaiDan t where 1=1";
		List<TCaiDan> toList = caiDanDao.find(hql);
		Collections.sort(toList, new CaiDanComparator()); //排序
		List<CaiDan> oList = new ArrayList<CaiDan>();
		oList = changeModel(toList);
		return oList;
	}
	
	private String addWhere(CaiDan caiDan, String hql, List<Object> params) {
		if(caiDan.getShangJiCDBH()!=null && !"".equals(caiDan.getShangJiCDBH())) {
			hql += "and t.pCaiDan.bianHao = ?";
			params.add(caiDan.getShangJiCDBH());
		}
		if(caiDan.getLuJing() != null && !"".equals(caiDan.getLuJing())) {
			hql += "and t.luJing like ?";
			params.add("%" + caiDan.getLuJing() + "%");
		}
		if(caiDan.getCaiDanMC() != null && !"".equals(caiDan.getCaiDanMC())) {
			hql += "and t.caiDanMC like ?";
			params.add("%" + caiDan.getCaiDanMC() + "%");
		}
		if(caiDan.getZhuangTai() != null && !"".equals(caiDan.getZhuangTai())) {
			hql += "and t.zhuangTai = ?";
			params.add(caiDan.getZhuangTai());
		}
 		return hql;
	}
	
	private String addOrder(CaiDan caiDan, String hql) {
		if (caiDan.getSort() != null) {
			hql += " order by " + caiDan.getSort() + " " + caiDan.getOrder();
		}
		return hql;
	}
	
	public List<CaiDan> changeModel(List<TCaiDan> tCaiDan ){
		Cache cac = CacheManager.getContent("zhuangTai");
		List<DictionaryDetail> ddList = new ArrayList();
		if(cac != null) {
			ddList = (List<DictionaryDetail>) cac.getValue();
		}
		List<CaiDan> list = new ArrayList<CaiDan>();
		for(TCaiDan to:tCaiDan){
			CaiDan o = new CaiDan();
			BeanUtils.copyProperties(to, o);
			TCaiDan pm = to.getpCaiDan();
			if(pm != null) {
				o.setShangJiCDBH(pm.getBianHao());
				o.setShangJiCDMC(pm.getCaiDanMC());
				//如果有路径地址就认为该菜单节点为子节点
				o.setIdLeaf(to.getLuJing() != null && !to.getLuJing().equals("") ? "1" : "0");
			}
			for(DictionaryDetail dd : ddList) {
				if(to.getZhuangTai() != null) {
					if(dd.getDetailID().equals(to.getZhuangTai().toString())) {
						o.setZhuangTaiStr(dd.getDetailName());
						break;
					}
				}
			}
			list.add(o);
		}
		return list;
	}


}
